#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" By Martin Senande-Rivera
    For Towards and atmosphere more favourable to firestorm development in Europe """

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from scipy import stats
import matplotlib.ticker as mtick
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.ticker import FormatStrFormatter


font = {'size'   : 5}
matplotlib.rc('font', **font)

# Read files
events = pd.read_csv('../1-Fire_simulations/simulations_results.csv', parse_dates=['Date'])


x = ['downdraft_1000max (m s-1)']
y = ['AREA_frac','SMOKEtop (m)','UVdif_max (m s-1)','CLOUD (kg kg-1)']
ylabel = ['Area fraction','Smoke height ($m$)','Surface wind difference ($m\cdot s^{-1}$)','Cloud ($g\cdot kg^{-1}$)']


fig = plt.figure(figsize=(6,4))
gs = fig.add_gridspec(2,2)

# Mean cloud water content g/kg
ax1 = fig.add_subplot(gs[0,0])
X = x[0]
Y = y[3]
yi=3
X_f03 = events[(events['FUEL']==3)].reset_index()[X].dropna()
Y_f03 = events[(events['FUEL']==3)].reset_index()[Y].dropna()*1000./(60.*60.*60.) # From g to kg (x1000.) and divided by the number of points of the domain (/60.*60.*60.) 
X_f06 = events[(events['FUEL']==6)].reset_index()[X].dropna()
Y_f06 = events[(events['FUEL']==6)].reset_index()[Y].dropna()*1000./(60.*60.*60.)
X_f10 = events[(events['FUEL']==10)].reset_index()[X].dropna()
Y_f10 = events[(events['FUEL']==10)].reset_index()[Y].dropna()*1000./(60.*60.*60.)
X_f12 = events[(events['FUEL']==12)].reset_index()[X].dropna()
Y_f12 = events[(events['FUEL']==12)].reset_index()[Y].dropna()*1000./(60.*60.*60.)
X_f13 = events[(events['FUEL']==13)].reset_index()[X].dropna()
Y_f13 = events[(events['FUEL']==13)].reset_index()[Y].dropna()*1000./(60.*60.*60.)
plt.plot(X_f03, Y_f03, 'o', markersize=3, zorder=5, color='b', label='Tall grass')
plt.plot(X_f06, Y_f06, 'o', markersize=3, zorder=4, color='g', label='Shrub')
plt.plot(X_f10, Y_f10, 'o', markersize=3, zorder=3, color='y', label='Timber litter')
plt.plot(X_f12, Y_f12, 'o', markersize=3, zorder=2, color='r', label='Medium logging slash')
plt.plot(X_f13, Y_f13, 'o', markersize=3, zorder=1, color='darkred', label='Heavy logging slash')
plt.xlabel('Downdraft ($m\cdot s^{-1}$)')
plt.ylabel(ylabel[yi])
plt.grid(True)
plt.legend()
ax1.text(-0.07, 1.07, 'a',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=ax1.transAxes)
plt.gcf()

# Maximum surface wind difference m s-1
ax2 = fig.add_subplot(gs[0,1])
X = x[0]
Y = y[2]
yi=2
X_f03 = events[(events['FUEL']==3)].reset_index()[X].dropna()
Y_f03 = events[(events['FUEL']==3)].reset_index()[Y].dropna()
X_f06 = events[(events['FUEL']==6)].reset_index()[X].dropna()
Y_f06 = events[(events['FUEL']==6)].reset_index()[Y].dropna()
X_f10 = events[(events['FUEL']==10)].reset_index()[X].dropna()
Y_f10 = events[(events['FUEL']==10)].reset_index()[Y].dropna()
X_f12 = events[(events['FUEL']==12)].reset_index()[X].dropna()
Y_f12 = events[(events['FUEL']==12)].reset_index()[Y].dropna()
X_f13 = events[(events['FUEL']==13)].reset_index()[X].dropna()
Y_f13 = events[(events['FUEL']==13)].reset_index()[Y].dropna()
plt.plot(X_f03, Y_f03, 'o', markersize=3, zorder=5, color='b', label='Tall grass')
plt.plot(X_f06, Y_f06, 'o', markersize=3, zorder=4, color='g', label='Shrub')
plt.plot(X_f10, Y_f10, 'o', markersize=3, zorder=3, color='y', label='Timber litter')
plt.plot(X_f12, Y_f12, 'o', markersize=3, zorder=2, color='r', label='Medium logging slash')
plt.plot(X_f13, Y_f13, 'o', markersize=3, zorder=1, color='darkred', label='Heavy logging slash')
plt.xlabel('Downdraft ($m\cdot s^{-1}$)')
plt.ylabel(ylabel[yi])
plt.grid(True)
plt.legend()
ax2.text(-0.07, 1.07, 'b',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=ax2.transAxes)
plt.gcf()

# Area fraction
ax3 = fig.add_subplot(gs[1,0])
X = x[0]
Y = y[0]
yi=0
X_f03 = events[(events['FUEL']==3)].reset_index()[X].dropna()
Y_f03 = events[(events['FUEL']==3)].reset_index()[Y].dropna()
X_f06 = events[(events['FUEL']==6)].reset_index()[X].dropna()
Y_f06 = events[(events['FUEL']==6)].reset_index()[Y].dropna()
X_f10 = events[(events['FUEL']==10)].reset_index()[X].dropna()
Y_f10 = events[(events['FUEL']==10)].reset_index()[Y].dropna()
X_f12 = events[(events['FUEL']==12)].reset_index()[X].dropna()
Y_f12 = events[(events['FUEL']==12)].reset_index()[Y].dropna()
X_f13 = events[(events['FUEL']==13)].reset_index()[X].dropna()
Y_f13 = events[(events['FUEL']==13)].reset_index()[Y].dropna()
plt.plot(X_f03, Y_f03, 'o', markersize=3, zorder=5, color='b', label='Tall grass')
plt.plot(X_f06, Y_f06, 'o', markersize=3, zorder=4, color='g', label='Shrub')
plt.plot(X_f10, Y_f10, 'o', markersize=3, zorder=3, color='y', label='Timber litter')
plt.plot(X_f12, Y_f12, 'o', markersize=3, zorder=2, color='r', label='Medium logging slash')
plt.plot(X_f13, Y_f13, 'o', markersize=3, zorder=1, color='darkred', label='Heavy logging slash')
plt.xlabel('Downdraft ($m\cdot s^{-1}$)')
plt.ylabel(ylabel[yi])
plt.grid(True)
plt.legend()
ax3.text(-0.07, 1.07, 'c',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=ax3.transAxes)
plt.gcf()

# Smoke top height m
ax4 = fig.add_subplot(gs[1,1])
X = x[0]
Y = y[1]
yi=1
X_f03 = events[(events['FUEL']==3)].reset_index()[X].dropna()
Y_f03 = events[(events['FUEL']==3)].reset_index()[Y].dropna()
X_f06 = events[(events['FUEL']==6)].reset_index()[X].dropna()
Y_f06 = events[(events['FUEL']==6)].reset_index()[Y].dropna()
X_f10 = events[(events['FUEL']==10)].reset_index()[X].dropna()
Y_f10 = events[(events['FUEL']==10)].reset_index()[Y].dropna()
X_f12 = events[(events['FUEL']==12)].reset_index()[X].dropna()
Y_f12 = events[(events['FUEL']==12)].reset_index()[Y].dropna()
X_f13 = events[(events['FUEL']==13)].reset_index()[X].dropna()
Y_f13 = events[(events['FUEL']==13)].reset_index()[Y].dropna()
plt.plot(X_f03, Y_f03, 'o', markersize=3, zorder=5, color='b', label='Tall grass')
plt.plot(X_f06, Y_f06, 'o', markersize=3, zorder=4, color='g', label='Shrub')
plt.plot(X_f10, Y_f10, 'o', markersize=3, zorder=3, color='y', label='Timber litter')
plt.plot(X_f12, Y_f12, 'o', markersize=3, zorder=2, color='r', label='Medium logging slash')
plt.plot(X_f13, Y_f13, 'o', markersize=3, zorder=1, color='darkred', label='Heavy logging slash')
plt.xlabel('Downdraft ($m\cdot s^{-1}$)')
plt.ylabel(ylabel[yi])
plt.grid(True)
plt.legend()
ax4.text(-0.07, 1.07, 'd',family='sans-serif',weight='bold',size=7, horizontalalignment='left', verticalalignment='top',transform=ax4.transAxes)
plt.gcf()

# Save figure
plt.subplots_adjust(top = 0.9, bottom=0.1, hspace=0.3, wspace=0.3)
fig.savefig('Figure1.png',dpi=300,bbox_inches='tight')   
fig.savefig('Figure1.pdf',bbox_inches='tight')   

